package com.neteast.androidclient.contact.adapter;

import java.util.ArrayList;
import java.util.Arrays;
import com.neteast.androidclient.contact.R;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;

public class GroupAdapter extends BaseAdapter {
	private ArrayList<String> data;
	private LayoutInflater inflater;
	private String checkedValue;
	private Button contactGroup;
	public GroupAdapter(Context context,ArrayList<String> data,Button contactGroup) {
		this.data=data;
		inflater = LayoutInflater.from(context);
		this.contactGroup=contactGroup;
		groupValues.clear();
	}
	public GroupAdapter(Context context,ArrayList<String> data,Button contactGroup,String checkedValue) {
		this(context, data,contactGroup);
		this.checkedValue=checkedValue;
		groupValues.addAll(Arrays.asList(checkedValue.split(",")));
	}
	public int getCount() {
		return data.size();
	}

	public String getItem(int position) {
		return data.get(position);
	}

	public long getItemId(int position) {
		return position;
	}
	
	public String getCheckedValue() {
		return checkedValue;
	}
	public void setCheckedValue(String checkedValue) {
		this.checkedValue = checkedValue;
		groupValues.clear();
		if (!TextUtils.isEmpty(checkedValue)) {
			groupValues.addAll(Arrays.asList(checkedValue.split(",")));
			notifyDataSetChanged();
		}
	}
	public View getView(int position, View convertView, ViewGroup parent) {
		convertView = inflater.inflate(R.layout.group_item, null);
		final TextView groupLabel = (TextView) convertView.findViewById(R.id.group_label);
		CheckBox groupHander = (CheckBox) convertView.findViewById(R.id.group_hander);
		groupLabel.setText(data.get(position));
		if (!groupValues.isEmpty()) {
			int size = groupValues.size();
			for (int i = 0; i < size; i++) {
				if (groupValues.get(i).equals(data.get(position))) {
					groupHander.setChecked(true);
					break;
				}
			}
		}
		groupHander.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				String s = groupLabel.getText().toString();
				if (isChecked&&!groupValues.contains(s)) {
					groupValues.add(s);
				}else if (!isChecked&&groupValues.contains(s)) {
					groupValues.remove(s);
				}
				final ArrayList<String> list=groupValues;
				int size = list.size();
				StringBuilder sb=new StringBuilder();
				for(int i=0;i<size;i++){
					String v = list.get(i);
					if (!TextUtils.isEmpty(v)) {
						sb.append(v).append(",");
					}
				}
				if (sb.length()>0) {
					sb.deleteCharAt(sb.length()-1);
				}
				contactGroup.setText(sb.toString());
			}
		});
		return convertView;
	}
	public static ArrayList<String> groupValues=new ArrayList<String>();
}
